package com.tmxk.controller.role;

import com.tmxk.constant.Result;
import com.tmxk.po.SysRole;
import com.tmxk.service.role.SysRoleService;
import com.tmxk.utils.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    @GetMapping("/role")
    @ApiOperation(notes = "查询所有角色",value = "查询所有角色")
    public ResultData queryAllRoleByInfo(SysRole role){
        List<SysRole> sysRoles = sysRoleService.queryAllRoleByInfo(role);
        Long count = sysRoleService.selectCount(role);
        return new ResultData<>(Result.SUCCESS.getCode(),"", count.intValue(),sysRoles);
    }

    @GetMapping("/role/{roleId}")
    @ApiOperation(notes = "查询所有角色",value = "查询所有角色")
    public ResultData queryRoleById(@PathVariable("roleId") Integer roleId){
        SysRole sysRole = sysRoleService.queryRoleById(roleId);
        return new ResultData<>(Result.SUCCESS.getCode(), "", Arrays.asList(sysRole));
    }

  /*  @ResponseBody : 圈在方法头上,将服务端的数据封装json格式响应给客户端(前端)
      @RequestBody : 放在方法形参前,将客户端发送的对象参数封装成json格式(请求的前端参数,以json格式)
    */
    @PostMapping("/role")
    @ApiOperation(notes = "保存角色",value = "保存角色")
    public ResultData saveSysRole(SysRole role){
        int code = sysRoleService.saveSysRole(role);
        code = code > 0 ? 0 : code;
        return new ResultData<>(code, "操作成功!!!");
    }

    @DeleteMapping("/role/{roleId}")
    @ApiOperation(notes = "删除角色",value = "删除角色")
    public ResultData deleteSysRoleById(@PathVariable("roleId") Integer roleId){
        int code = sysRoleService.deleteSysRoleById(roleId);
        code = code > 0 ? 0 : code;
        return new ResultData<>(code, "操作成功!!!");
    }

    @PutMapping("/role")
    @ApiOperation(notes = "更新角色",value = "更新角色")
    public ResultData updateSysRole(SysRole role){
        int code = sysRoleService.updateSysRole(role);
        code = code > 0 ? 0 : code;
        return new ResultData<>(code, "操作成功!!!");
    }


}
